package com.ibeetl.admin.core.web.util;

import com.ibeetl.admin.core.rbac.tree.AreaItem;
import com.ibeetl.admin.core.service.CoreAreaService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.web.dto.AreaNodeView;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.ext.simulate.WebSimulate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 没有找到匹配的Controller
 * 
 * @author xiandafu
 *
 */
@Controller
public class AreaUtilController {
	@Autowired
	WebSimulate webSimulate;
	Log log = LogFactory.getLog(AreaUtilController.class);
	private static final String MODEL = "/core";
	@Autowired private CoreAreaService coreAreaService;
	@Autowired private CorePlatformService platformService;
	/**
	 * 获取系统的栏目树
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(MODEL + "/area/tree.json")
	@ResponseBody
	public List<AreaNodeView> getAreaTree(HttpServletRequest request, HttpServletResponse response, String parentAreaCode) throws IOException {
		ClassPathResource cpr = new ClassPathResource("/area.json");
		if (cpr.exists()) {
			response.setContentType("application/json; charset=UTF-8");
			//response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(cpr.getFilename(),"UTF-8"));
			OutputStream os = response.getOutputStream();
			FileInputStream input = null;
			try {
				input = new FileInputStream(cpr.getFile());
				byte[] buf = new byte[1024];
				int bytesRead;
				while ((bytesRead = input.read(buf)) > 0) {
					os.write(buf, 0, bytesRead);
				}
			}catch(Exception ex) {
				throw new PlatformException("下载文件失败"+ex);
			}
			finally {
				try {
					input.close();
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			return null;
		}
		AreaItem areaItem = platformService.buildArea(parentAreaCode);
		List<AreaNodeView> view = this.build(areaItem);
		//FileUtils.writeStringToFile(new File("c:/tree.json"), JSON.toJSONString(view));
		return view;
	}

	private List<AreaNodeView> build(AreaItem node) {
		List<AreaItem> list = node.getChildren();
		if (list.size() == 0) {
			return Collections.EMPTY_LIST;
		}

		List<AreaNodeView> views = new ArrayList<AreaNodeView>(list.size());

		for (AreaItem item : list) {
			AreaNodeView view = new AreaNodeView();
			if (item.getParent() != null) {
				view.setParentCode(item.getParent().getAreaCode());
			}
			view.setLevel(item.getData().getLevel());
			view.setAreaCode(item.getData().getAreaCode());
			view.setZipCode(item.getData().getZipCode());
			view.setCityCode(item.getData().getCityCode());
			view.setShortName(item.getData().getShortName());
			view.setMergerName(item.getData().getMergerName());
			view.setPinyin(item.getData().getPinyin());

			view.setName(item.getData().getName());
			List<AreaNodeView> children = this.build(item);
			view.setChildren(children);
			views.add(view);
		}
		return views;
	}
}
